package com.EWS.controller;

import com.EWS.model.UserDto;
import com.EWS.service.UserService;
import com.EWS.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 与前端交互在设置页面进行各种操作
@WebServlet("/SettingServlet")
public class SettingServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 通过前端登录时提交的会话信息获取此时登录的用户名
        HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
        String userName = (String) session.getAttribute("userName");
        // 获取前端提交的密码
        String password = req.getParameter("password");
        // 通过用户名获取该用户的信息，后边要使用该用户原来的盐值来修改密码
        UserDto userDto = userService.getUserByName(userName);
        int result = userService.updatePassword(userDto,password);
        if(result>0){
            // 密码修改成功，刷新设置界面
            req.getRequestDispatcher("setting.jsp").forward(req,resp);
        }
        else{
            System.out.println("密码修改失败");
        }
    }
}
